﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Results;
using XQ.RequestOfficeSupplies.BLL.home;
using XQ.RequestOfficeSupplies.DAL;
using XQ.RequestOfficeSupplies.DAL.DAL.Common;
using XQ.RequestOfficeSupplies.DAL.Model;
using XQ.RequestOfficeSupplies.DAL.Model.Business;
using XQ.RequestOfficeSupplies.DAL.Models;
using XQ.RequestOfficeSupplies.WebAPI.Common;
using XQ.RequestOfficeSupplies.WebAPI.Filter;
using XQ.RequestOfficeSupplies.WebAPI.Models;

namespace XQ.RequestOfficeSupplies.WebAPI.Controllers
{
    /// <summary>
    /// 主页信息
    /// </summary>
    /// 
    [FormAuthenticationFilter]
    public class IndexController : ApiController
    {
        /// <summary>
        /// 根据用户获取菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Route("Api/Index/GetMenu")]
        public ResultModel GetMenu(int userId)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = new TokenModel();
                //获取用户信息
                var authorization = Request.Headers.Authorization;
                if (authorization != null)
                {
                    if (authorization.Scheme == "Bearer")
                    {
                        string tokenStr = authorization.Parameter;
                        token = TokenHelper.DecryptToken(tokenStr);
                    }
                }
                IndexBll indexBll = new IndexBll();

                MenuModel menus = indexBll.GetMenusByUser(token.UID, token.DepartmentID);
                
                resultModel.success = true;
                resultModel.code = "10000";
                resultModel.message = "执行成功";
                resultModel.data = menus;
                
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.code = "10001";
                resultModel.message = "执行失败";
                resultModel.data = ex.Message;
            }

            return resultModel;
        }

        /// <summary>
        /// 获取“我的”页面信息
        /// </summary>
        /// <returns></returns>
        [Route("api/Index/GetRentMyInfo")]
        [HttpGet]
        public ResultModel GetRentMyInfo()
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "获取“我的”页面信息", "", "", "", "api/Index/GetMyInfo：" + JsonConvert.SerializeObject(token));

                //resultModel.data = contractBLL.GetContractByOpenid(token);
                if (resultModel.data == null)
                {
                    resultModel.success = false;
                    resultModel.code = "10001";
                    resultModel.message = "没有找到对应的数据";
                    return resultModel;
                }
            }
            catch (Exception ex)
            {
                LogBll.Instance.WriteErrLog("", 0, "获取“我的”页面信息报错", "", "", "", ex.InnerException != null ? ex.InnerException.Message : ex.Message, "", "IndexController", "GetMyInfo");
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }
    }
}